#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
import time

import requests
#cron:6 0-23 * * *
from bs4 import BeautifulSoup
import sys
import io
import urllib3
urllib3.disable_warnings()

#sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="gbk")  ## encoding
def tuisong():
    urlwx = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
    corpid = 'wwbe7a837b20998a38'
    corpsecret = 'LpfiGTtySEW4NW40hDMwfew4INffZY-DIpGbL5h_q-A'

    # 获取token时，携带企业id和secret(注册企业号时，后台可查)
    values = {'corpid': corpid, 'corpsecret': corpsecret}
    reqwx = requests.get(url=urlwx, params=values)
    datawx = json.loads(reqwx.text)
    access_token = datawx["access_token"]
    print(access_token)
    urlwx2 = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
    # 图文
    tuwen = {
        "touser": "@all",
        "toparty": "",
        "totag": "",
        "msgtype": "mpnews",
        "agentid": 1000004,
        "mpnews": {
            "articles": [
                {
                    "title": "gmu官网更新啦",
                    "thumb_media_id": "20inlgKNG8LegISqzm48t-_eiAH2vRCjpk9rcBPAPOMzWqOQYcw4GyZCY5uqnIoci",
                    "author": "帅气的林老师",
                    "content_source_url": "",
                    "content": html6,
                    "digest": html6
                }
            ]
        },
        "safe": 0,
        "enable_id_trans": 0,
        "enable_duplicate_check": 0,
        "duplicate_check_interval": 1800
    }
    # 文本
    wenben = {
        "touser": "@all",
        "toparty": "",
        "totag": "",
        "msgtype": "text",
        "agentid": 1000004,
        "text": {"content": html6},
        "safe": 0,
        "enable_id_trans": 0,
        "enable_duplicate_check": 0,
        "duplicate_check_interval": 1800
    }
    # 文本卡片
    wenbenkapian = {
        "touser": "@all",
        "toparty": "",
        "totag": "",
        "msgtype": "textcard",
        "agentid": 1000004,
        "textcard": {
            "title": "安全知识竞赛",
            "description": html6,
            "url": " ",
            "btntxt": "更多"
        },
        "enable_id_trans": 0,
        "enable_duplicate_check": 0,
        "duplicate_check_interval": 1800
    }
    # wxtui = requests.post(url=urlwx2, json=tuwen)
    kapian = requests.post(url=urlwx2, json=tuwen)

def read():
    with open("gmucahe.txt",'r',encoding='utf8') as f:
        #print(f.read())
        return f.read()
        f.close()

def xin():
    url = 'http://yjs.gmu.cn/zsgz.htm'
    headers = {'Host': 'yjs.gmu.cn', 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.95 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Referer': 'http://yjs.gmu.cn/zsgz.htm', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8'}
    cookies = {'JSESSIONID': ''}
    data = {}

    html = requests.get(url, headers=headers, verify=False, cookies=cookies)
    html = requests.get(url, headers=headers, verify=False, cookies=cookies)
    #print(html.status_code)
    #print(len(html.text))
    #print(html.text)
    a = html.text
    htm = a.replace(u'\xb5', u'')
    htm1 = htm.replace(u'\xa3', u'')
    htm2 = htm1.replace(u'\xe5', u'')
    htm3 = htm2.replace(u'\x8d', u'')
    bs = BeautifulSoup(htm3, 'lxml')
    #< h2 class ="cleafix" >
    div = bs.find('h2', {'class': "cleafix"})
    title = div.find('a')['href']
    b= 'http://yjs.gmu.cn/info/1075/2852.htm'
    html6 = f'http://yjs.gmu.cn/{title}'
    return html6
def write():
    with open('gmucahe.txt','w+',encoding='utf8') as f:
        f.write(xin())
        f.close()

if xin()==read():
    print("官网还未更新！")
else:
    print("官网已更新，开始推送！")
    html6=xin()
    tuisong()
    write()
